#include "ServerMoon.h"
ServerMoon::ServerMoon(int pMoonId, int pStartingPopulation)
{
	moonId = pMoonId;
	playerId = -1;
	population = pStartingPopulation;
	currentProductionTask = NULL;
	currentResearchTask = NULL;
	missileResearchLevel = 1;
	shieldResearchLevel = 1;
	reproduction = 50;
	production = 25;
	research = 25;
	readyMissile = NULL;
	readyShield = NULL;
}

void ServerMoon::assign(int pPlayerId)
{
	playerId = pPlayerId;
}
int ServerMoon::getPlayerId() { return playerId;}
int ServerMoon::getMoonId() { return moonId;}
int ServerMoon::getBuildProgress()
{
	if(currentProductionTask != NULL)
	{
		return (*currentProductionTask).getProgress();
	}
	return -1;
}

int ServerMoon::getResearchProgress()
{
	if(currentResearchTask != NULL)
	{
		return (*currentResearchTask).getProgress();
	}
	return -1;
}

int ServerMoon::getPopulation(){return (int)population;};

void ServerMoon::setPopulation(int newPopulation)
{
	population = newPopulation;
}

string ServerMoon::getCurrentProductionTaskName()
{
	if(currentProductionTask != NULL)
	{
		return (*currentProductionTask).getTaskName();
	}
	return "No task set.";
}

string ServerMoon::getCurrentResearchTaskName()
{
	if(currentResearchTask != NULL)
	{
		return (*currentResearchTask).getTaskName();
	}
	return "No task set.";
}

void ServerMoon::startBuild(int task, int level)
{
	if(task == PRODUCTION_DO_NOTHING)
	{
		currentProductionTask = NULL;
	}
	else if(task == PRODUCTION_MISSILE)
	{
		currentProductionTask = new ProductionTask(task, level, MISSILE_TIMES[level]);
	}	
	else if(task == PRODUCTION_SHIELD)
	{
		currentProductionTask = new ProductionTask(task, level, SHIELD_TIMES[level]);
	}
}

void ServerMoon::startResearch(int task, int level)
{
	if(task == RESEARCH_DO_NOTHING)
	{
		currentResearchTask = NULL;
	}
	else if(task == RESEARCH_MISSILE)
	{
		currentResearchTask = new ResearchTask(task, level, MISSILE_TIMES[level]);
	}	
	else if(task == RESEARCH_SHIELD)
	{
		currentResearchTask = new ResearchTask(task, level, SHIELD_TIMES[level]);
	}
}

void ServerMoon::update(int numSteps)
{
	//these are the people working on these tasks
	int researchPopulation = (int)(((.01)*research) * population);
	int productionPopulation = (int)(((.01)*production) * population);
	int reproductionPopulation = (int)(((.01)*reproduction) * population);
	
	//increase  the population by 1 for every 10000 people reproducing
	//probably will need to drastically decrease...
	population += ((reproductionPopulation/10000) * numSteps);
	
	double pttc = (*currentProductionTask).getTimeToComplete();
	double newpttc = pttc - ((productionPopulation/10000) * numSteps);
	(*currentProductionTask).setTimeToComplete(newpttc);
	
	if((*currentProductionTask).isCompleted())
	{
		if((*currentProductionTask).getTaskId() == PRODUCTION_MISSILE)
		{
			readyMissile = new Missile((*currentProductionTask).getPowerLevel());
			free(currentProductionTask);
			currentProductionTask = NULL;
		}
		else if ((*currentProductionTask).getTaskId() == PRODUCTION_SHIELD)
		{
			readyShield = new Shield((*currentProductionTask).getPowerLevel());
			free(currentProductionTask);
			currentProductionTask = NULL;
		}
	}


	double rttc = (*currentResearchTask).getTimeToComplete();
	double newrttc = rttc - ((researchPopulation/10000) * numSteps);
	(*currentResearchTask).setTimeToComplete(newrttc);

	if((*currentResearchTask).isCompleted())
	{
		if((*currentResearchTask).getTaskId() == RESEARCH_MISSILE)
		{
			missileResearchLevel = (*currentResearchTask).getResearchLevel();
			free(currentResearchTask);
			currentResearchTask = NULL;
		}
		else if ((*currentResearchTask).getTaskId() == RESEARCH_SHIELD)
		{
			shieldResearchLevel = (*currentResearchTask).getResearchLevel();
			free(currentResearchTask);
			currentResearchTask = NULL;
		}
	}

}

int ServerMoon::getReproduction(){return reproduction;}
int ServerMoon::getProduction(){return production;}
int ServerMoon::getResearch(){return research;}
void ServerMoon::allocatePopulation(int pReproduction, int pProduction, int pResearch)
{
	//do comm request to update these values
	reproduction = pReproduction;
	production = pProduction;
	research = pResearch;
}

int ServerMoon::getMissileResearchLevel()
{
	return missileResearchLevel;
}

int ServerMoon::getShieldResearchLevel()
{
	return shieldResearchLevel;
}

Missile * ServerMoon::getReadyMissile()
{
	return readyMissile;
}

Shield * ServerMoon::getReadyShield()
{
	return readyShield;
}